package cn.mogugo.util.tools;

public class Encrypt {
	public Encrypt() {

    }


    
    /**
     * 普通加密字符串
     * 
     * @param encryptstring1
     * @param encryptstring2
     * @return String
     */
    public static String encrypt(String encryptstring1, String encryptstring2) {
        int strcount = 0; //字符串ASCII码的总和
        int newpasswordlength = 0; //生成的密码长度
        int seed1 = 0; //种子
        int seed2 = 0;
        String str = "";
        String newpassword = ""; //生成的密码
        char newchar;
        int newcharint = 0;
        int thechar = 0;

        str = encryptstring2 + encryptstring1;

        if (str.length() > 0) {
            for (int i = 0; i < str.length(); i++) {
            	
            	
                strcount = strcount + str.charAt(i);
            }
            
            
            
            newpasswordlength = (strcount * strcount) / str.length() + str.length();
            
            int temp = 0;
            int temp2 = 0;
            while ((newpasswordlength <= 0) || (newpasswordlength > 100)) {
                temp = temp + str.length();
                temp2 = temp + temp2;
                newpasswordlength = 100;
            }

            for (int i = 0; i < encryptstring1.length(); i++) {
                seed1 = seed1 + encryptstring1.charAt(i);
            }
            for (int i = 0; i < encryptstring2.length(); i++) {
                seed2 = seed2 + encryptstring2.charAt(i);
            }

            for (int i = 0; i < newpasswordlength; i++) {

                if (i < str.length()) {
                    thechar = str.charAt(i);
                } else {
                    thechar = str.charAt(i % (str.length() - 1))
                            + str.charAt((str.length() - 1)
                                    - (i % str.length()));
                }
                newcharint = thechar * (seed1 * (i + 1) + thechar);
                newcharint = newString(newcharint);
                
                
                //newcharint = thechar * (seed2 * (i + 1) + thechar);
                //newcharint = newString(newcharint);
                newchar = (char) newcharint;
                
                //System.out.println(newchar);
                
                newpassword = newpassword + String.valueOf(newchar);
            }
            
            
            
            newpassword = newpassword.substring((encryptstring2.length() + encryptstring1.length()) % 100);
            return (newpassword);
        } else {
            return ("空字符串不能被加密");
        }
    }

    private static int newString(int charint) {
        while ((charint > 127) || (charint < 32)) {
            while (charint > 127) {
                charint = (charint - charint/2+1) / 2;
                while(charint==60||charint==62||charint==34||charint==32||charint==39){
                 charint = charint + 1;
                }
            }
            while (charint < 32) {
                charint = (charint + charint/2-1) * 2;
                while(charint==60||charint==62||charint==34||charint==32||charint==39){
                 charint = charint + 1;
                }
            }
        }
        return (charint);
    }

    public static void main(String[] args) {
        String theusername = "1abc1";
        String thepassword = "1def1";
        String theusername1 = "1def1";
        String thepassword1 = "1abc1";
        String newstring=Encrypt.encrypt(theusername,thepassword);
        System.out.println("newstring="+newstring);
        String newstringa=Encrypt.encrypt(theusername1,thepassword1);
        System.out.println("newstringa="+newstringa);
    }



}
